package com.game.objects;

import org.jdom.Element;

import com.game.actions.ShootAction;
import com.game.controllers.DefaultEnemyController;
import com.game.controllers.ShootController;
import com.game.controllers.TowerEnemyController;
import com.game.level.Terrain;
import com.game.system.Game;
import com.game.system.Utils;
import com.jme.bounding.BoundingBox;
import com.jme.input.action.InputActionEvent;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Controller;
import com.jme.scene.Node;
import com.jme.scene.SharedMesh;
import com.jme.scene.Spatial;
import com.jme.scene.TriMesh;

public class Enemy{

	Spatial model;
	float speed;
	float shootRate = 2f;
	float shootCounter;
	TriMesh bulletModel;
	private int bulletCounter;
	private Terrain terra;
	private Quaternion rotation;
	private boolean active=false;
	private Element info;
	
	public Enemy(Element enemyInfo, Terrain terra){
		this.terra=terra;
		this.info=enemyInfo;
		model = Utils.loadModel(enemyInfo.getAttributeValue("src"));
		model.setLocalTranslation(Float.parseFloat(enemyInfo.getAttributeValue("x")),
							      0f,
								  Float.parseFloat(enemyInfo.getAttributeValue("z")));

		model.setModelBound(new BoundingBox());
		model.updateModelBound();
		
		speed= Float.parseFloat(enemyInfo.getAttributeValue("speed"));
		
		terra.attachChild(model);
		String ys = enemyInfo.getAttributeValue("y");
		if(ys.equals("auto")){
			terra.updateModelY(model);
		}else{
			model.getLocalTranslation().setY(Float.parseFloat(ys));
		}

		rotation = model.getLocalRotation();
		bulletModel = (TriMesh)Utils.loadModel("shot");
	}
	
	public void activate(){
		if(!isActive()){
			String control = info.getAttributeValue("control");
			if(control.equals("tower")){
				model.addController(new TowerEnemyController(this));
			}else{
				model.addController(new DefaultEnemyController(this));
			}
			active=true;
		}
	}
	
	public boolean isActive(){
		return active;
	}
	

	public Spatial getEnemy() {
		return model;
	}

	public float getSpeed() {
		return speed;
	}

	public void shoot(){
		SharedMesh bullet = new SharedMesh("Enemy-shot"+bulletCounter++, bulletModel);
		bullet.setModelBound(new BoundingBox());
		bullet.updateModelBound();
		bullet.setLocalRotation(new Quaternion(rotation));
		bullet.setRenderState(Utils.createMaterialState(ColorRGBA.red));
		bullet.updateGeometricState(0, true);
		Node root = Game.getGameLogic().getRootNode();
		root.attachChild(bullet);
		bullet.addController(new ShootController(bullet,
				Game.getGameLogic().getEnvironment(),
				model.getWorldTranslation().add(rotation.getRotationColumn(2).normalize().mult(100)), 
				rotation.getRotationColumn(2)));
		bullet.updateRenderState();


	}
	
	public void update(float time){
		shootCounter+=time;
		if(shootCounter>=shootRate){
			shoot();
			shootCounter=shootCounter-shootRate;
		}
		
	}
	
	public boolean checkSimpleCollision(Spatial obj) {
		
		if(model.getWorldBound()!=null && model.getWorldBound().intersects(obj.getWorldBound())){
			destroy();
			return true;
		}
		return false;
	}
	
	public void destroy(){
		model.removeFromParent();
	}
	
	public void setRotation(Quaternion rotation){
		this.rotation=rotation;
	}
	
	public Terrain getTerrain(){
		return terra;
	}

	
}
